<template>
  <div>
              <van-button
            v-if="value"
            class="follow-btn"
            round
            size="small"
            @click="onFollow"
            :loading= "loading"
            >已关注</van-button
          >
          <van-button
            v-else
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            @click="onFollow"
            :loading= "loading"
            >关注</van-button
          >
  </div>
</template>

<script>
import { addFollw, deleteFollw } from '@/api/user'
export default {
  name: 'FolloWd',
  medel: {
    prop: 'isFollowd', // 默认是value
    event: 'update-is_followed'
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onFollow () {
      this.loading = true // 展示关注按钮的loading状态
      try {
        if (this.value) {
          // 已关注,取消关注
          await deleteFollw(this.autId)
        } else {
          // 没有关注，添加关注
          // console.log('123', typeof this.autId)
          await addFollw(this.autId)
        }
        // 更新视图
        // this.article.is_followed = !this.article.is_followed
        // this.$emit('update-is_followed', !this.value)
        this.$emit('input', !this.value)
      } catch (err) {
        let message = '操作失败，请重试!'
        if (err.response && err.response.status === 400) {
          console.log(err)
          message = '你不能关注你自己！'
        }
        this.$toast(message)
      }
      // 因为最终都需要关闭，所有把loading状态再catch最后面
      this.loading = false // 关闭关注按钮的loading状态
    }
  }

}
</script>

<style>

</style>
